<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!--    地图引入-->
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <!--    地图组件-->
    <style type="text/css">
        body, html {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
            font-size: 14px;
        }

        #l-map {
            height: 300px;
            width: 100%;
        }

        #r-result {
            width: 100%;
        }
    </style>

    <script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&type=webgl&ak=3V1b9H41F7LOqEyF4hcFyVaWjqGwnwFS"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        .tangram-suggestion-main{
            z-index: 3001!important;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理
            <small>地址管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>


    <div class="app-container">
        <template>
            <el-tabs v-model="activeName" type="card">

                <el-tab-pane label="公司分布图" name="first">

                    <iframe name="right" class="el-main" src="addrTest.html" width="100%" height="580px"
                            frameborder="0"></iframe>

                </el-tab-pane>

                <el-tab-pane label="详细地址" name="second">

                    <div class="box">
                        <div class="filter-container">
                            <el-input placeholder="机构名称/地址" v-model="pagination.queryString" style="width: 200px;"
                                      class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                            <el-button @click="pagination.currentPage = 1 ; findPage()" class="dalfBut">查询</el-button>
                            <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                        </div>
                        <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                            <el-table-column type="index" align="center" label="序号"></el-table-column>
                            <el-table-column prop="name" label="地址名称" align="center"></el-table-column>
                            <el-table-column prop="telephone" label="机构电话" align="center"></el-table-column>
                            <el-table-column prop="longitude" label="经度" align="center"></el-table-column>
                            <el-table-column prop="latitude" label="纬度" align="center"></el-table-column>
                            <el-table-column prop="addrImg" label="机构图片" align="center"></el-table-column>
                            <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                            <el-table-column label="操作" align="center">
                                <template slot-scope="scope">
                                    <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑
                                    </el-button>
                                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                                </template>
                            </el-table-column>
                        </el-table>
                        <div class="pagination-container">
                            <el-pagination
                                    class="pagiantion"
                                    @current-change="handleCurrentChange"
                                    :current-page="pagination.currentPage"
                                    :page-size="pagination.pageSize"
                                    layout="total, prev, pager, next, jumper"
                                    :total="pagination.total">
                            </el-pagination>
                        </div>

                        <!-- 新增标签弹层 -->
                        <div class="add-form">
                            <el-dialog title="新增地址" :visible.sync="dialogFormVisible" @opened="getMap">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="机构名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="联系电话" prop="telephone">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row justify="center">
                                        <el-col>
                                            <el-tag type="success">单击地图自动填写经纬度</el-tag>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="经度">
                                                <el-input v-model="formData.longitude"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="纬度">
                                                <el-input v-model="formData.latitude"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="上传图片">
                                                <el-upload
                                                        class="avatar-uploader"
                                                        action="/address/uploadImg"
                                                        :auto-upload="autoUpload"
                                                        name="imgFile"
                                                        :show-file-list="false"
                                                        :on-success="handleAvatarSuccess"
                                                        :before-upload="beforeAvatarUpload">
                                                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                </el-upload>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="详细地址">
                                                <div id="r-result">
                                                    <el-input id="suggestId"  v-model="formData.address" type="text"></el-input>
                                                </div>
                                                <div id="searchResultPanel"></div>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>

                                <div id="l-map"></div>

                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisible = false">取消</el-button>
                                    <el-button type="primary" @click="handleAdd()">确定</el-button>
                                </div>
                            </el-dialog>
                        </div>

                        <!-- 修改标签弹层 -->
                        <div class="add-form">
                            <el-dialog title="编辑公司地址" :visible.sync="dialogFormVisibleEdit">
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="机构名称">
                                                <el-input v-model="formData.name" prop="name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="联系电话">
                                                <el-input v-model="formData.telephone" prop="telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="经度">
                                                <el-input v-model="formData.longitude"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="纬度">
                                                <el-input v-model="formData.latitude"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="上传图片">
                                                <el-upload
                                                        class="avatar-uploader"
                                                        action="/address/uploadImg"
                                                        :auto-upload="autoUpload"
                                                        name="imgFile"
                                                        :show-file-list="false"
                                                        :on-success="handleAvatarSuccess"
                                                        :before-upload="beforeAvatarUpload">
                                                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                                </el-upload>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="详细地址">
                                                <el-input id="suggestId2" v-model="formData.address" type="text"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                                <div slot="footer" class="dialog-footer">
                                    <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                                    <el-button type="primary" @click="handlerEdit()">确定</el-button>
                                </div>
                            </el-dialog>
                        </div>

                    </div>

                </el-tab-pane>

            </el-tabs>

        </template>
    </div>
</div>
</body>
<script type="application/javascript">
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//添加表单窗口中检查组列表数据
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisibleEdit: false,//控制修改窗口显示/隐藏
            rules: {//校验规则
                name: [{required: true, message: '机构名称为必填项', trigger: 'blur'}],
                telephone: [{required: true, message: '联系电话为必填项', trigger: 'blur'}]
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //文件上传成功后的钩子，result为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(result, file) {
                //提示上传结果
                this.$message({
                    type: result.flag ? "success" : "error",
                    message: result.message
                });
                //设置图片地址
                this.imageUrl = "http://health.zlf520.cn/" + result.data;
                //设置图片地址到体检套餐表单
                this.formData.img = result.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //在输入框获取地图提示
            getMap() {
                map = new BMapGL.Map("l-map");

                map.centerAndZoom("武汉", 12);                   // 初始化地图,设置城市和地图级别。

                map.enableScrollWheelZoom(true);
                //绑定地图单击事件,获取经纬度
                map.addEventListener('click' , function (e) {
                    // alert(e.latlng.lng+','+e.latlng.lat)
                    vue.$set(vue.formData,"longitude",e.latlng.lng);
                    vue.$set(vue.formData,"latitude",e.latlng.lat);
                });

                var ac = new BMapGL.Autocomplete({
                        "input": "suggestId", "location": map
                    });

                ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province + _value.city + _value.district + _value.street + _value.business;
                    }
                });

                var myValue;
                //onconfirm
                ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                    vue.$set(vue.formData,"address",myValue);
                    /*alert(e.latlng.lng);
                    alert(e.latlng.lat);*/
                    /*vue.$set(vue.formData,"longitude",e.latlng.lng);
                    vue.$set(vue.formData,"latitude",e.latlng.lat);*/
                    setPlace(myValue);
                });


            },
            // 弹出添加窗口
            handleCreate() {
                //展示弹出框
                this.dialogFormVisible = true;
                //重置表单数据
                this.resetForm();
                //重置体检套餐图片数据
                this.imageUrl = null;
                /*//发送异步请求获取检查组数据
                axios.get("/address/findAll").then(response => {
                    if (response.data.flag) {
                        this.tableData = response.data.data;
                        return;
                    }
                    this.$message.error(response.data.message);
                })*/
            },
            //添加
            handleAdd() {
                //发送异步请求 ,完成数据添加
                var url = "/address/addAddr";
                axios.post(url, this.formData).then(response => {
                    //添加成功
                    if (response.data.flag) {
                        //隐藏弹出框
                        this.dialogFormVisible = false;
                        //展示提示信息
                        this.$message.success(response.data.message);
                        return;
                    }
                    //添加失败
                    this.$message.error(response.data.message);

                }).finally(() => this.findPage())
            },
            //删除当前项
            handleDelete(row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = "/address/deleteById/" + row.id;
                    axios.delete(url).then(response => {
                        if (response.data.flag) {
                            this.findPage();
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        } else {
                            this.$message.error(response.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            //弹出编辑弹窗
            handleUpdate(row) {
                //显示编辑弹出框
                this.dialogFormVisibleEdit = true;
                //重置编辑弹出框数据
                this.resetForm();
                //加载套餐基本数据
                axios.get("/address/findById/" + row.id).then(response => {
                    if (!response.data.flag) {
                        this.$message.error(response.data.message);
                        return;
                    }
                    this.formData = response.data.data;
                    this.imageUrl = "http://health.zlf520.cn/" + this.formData.img;
                })
            },
            //编辑套餐数据
            handlerEdit() {
                ////发送异步请求
                var url = "/address/update";
                axios.put(url, this.formData).then(response => {
                    if (response.data.flag) {
                        this.$message.success(response.data.message);
                        this.dialogFormVisibleEdit = false;
                        return
                    }
                    this.$message.error(response.data.message);
                }).finally(() => this.findPage());
            },
            //分页查询
            findPage() {
                //发送异步请求查询分页数据
                let params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.get("/address/findPage", {params: params}).then(response => {
                    if (response.data.flag) {
                        this.dataList = response.data.data.list;
                        this.pagination.total = response.data.data.total;
                        /*this.$message({
                            message: response.data.message,
                            type: 'succcess'
                        });*/
                        return;
                    }

                    this.$message.error(response.data.message);
                })
            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },

            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //执行删除操作
            handleDelete(row) {
                this.$confirm("您确定要删除此记录吗?", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //发送异步请求删除体检套餐数据
                    axios.delete("/address/" + row.id).then(response => {
                        if (response.data.flag) {
                            this.$message.success(response.data.message);
                            return;
                        }
                        this.$message.error(response.data.message);
                    }).finally(() => this.findPage());
                });
            }
        }
    });


    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }

    function setPlace(myValue) {
        map.clearOverlays();    //清除地图上所有覆盖物
        function myFun() {
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            map.centerAndZoom(pp, 18);
            map.addOverlay(new BMapGL.Marker(pp));    //添加标注
        }
        var local = new BMapGL.LocalSearch(map, { //智能搜索
            onSearchComplete: myFun
        });
        local.search(myValue);
        //searchResultPanel
    }
</script>
</html>
